package edu.guigu.appquickstart.recyclerview.bean;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/4 10:54
 */
public class UserBean {
    private int id;
    private String login;
    private String avatar_url;
    private String html_url;

    public UserBean(String login, String avatar_url) {
        this.login = login;
        this.avatar_url = avatar_url;
    }

    public String getHtml_url() {
        return html_url;
    }

    public void setHtml_url(String html_url) {
        this.html_url = html_url;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getAvatar_url() {
        return avatar_url;
    }

    public void setAvatar_url(String avatar_url) {
        this.avatar_url = avatar_url;
    }

    public static ArrayList<UserBean> loadData(Context context) {
        // 读取
        String json = load(context);
        // 解析
        Gson gson = new Gson();
        TypeToken<ArrayList<UserBean>> typeToken = new TypeToken<>() {};
        ArrayList<UserBean> personBeans = gson.fromJson(json, typeToken.getType());

        return personBeans;
    }

    private static String load(Context context){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            // Assets资源文件读取
            InputStream inputStream = context.getAssets().open("users.json");
            InputStreamReader isr = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
            br.close();
            isr.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}